#include"utils.h"
#include<stdlib.h>
#include<string.h>

#define TITLE_SIZE 35

struct film {
    char title[TITLE_SIZE];
    int rating;
    struct film * next;
};

int main() {

    struct film * head = NULL;
    struct film * prev, * current;
    char input[TITLE_SIZE];

    // gather and store information
    puts("Enter first movie title:");
    while (s_gets(input, TITLE_SIZE) != NULL && input[0] != '\0') {
        // malloc memory to pointer current
        current = (struct film *) malloc(sizeof(struct film));
        if (head == NULL)   // first structure
            head = current;
        else 
            prev->next=current; // subsequent structures
        
        current->next = NULL;
        strcpy(current->title, input);
        puts("Enter your rating <0-10>:");
        scan("%d", &current->rating);
        while(getchar() != '\n') {
            continue;
        }
        puts("Enter next movie title (empty line to stop):");
        prev = current;
    }

    // show list of movies

    if (head == NULL) 
        printf("No data enter.");
    else
        printf("Here is the movie list: \n");
    
    current = head;
    while(current != NULL) {
        printf("Movie: %s Rating: %d\n", current->title, current->rating);
        // shift to next pointer
        current = current->next;
    }

    // program done, so free allocated memory
    current = head;
    while(current != NULL) {
        free(current);
        current = current->next;
    }

    printf("Bye.\n");
    return 0;
}
